const YoloDetector = require('../yolo');
const APP = require('../APP');
class index {
    androidBot = null;
    constructor(client) {
        this.GameClient = client;
        this.androidBot = this.GameClient.androidBot;
    }
    async run() {
        let loginInfo = this.GameClient.loginInfo;
        let data = await YoloDetector.screen(this.androidBot);

        let loginBtn = APP.findClass(data, 'login_btn_startGame');
        if (loginBtn) {
            console.log('已授权，点击开始游戏')
            await APP.clickRegion(this.androidBot, data[loginBtn - 1].region);
            return true;
        }

        let jude = false;
        console.log('开始登陆流程11111', loginInfo);
        let loginCount = APP.findClassCount(data, 'btn_text');
        let tryCount = 0;
        if (loginCount == 0) {
            while(true){
                if (loginCount == 0) {
                    data = await YoloDetector.screen(this.androidBot);
                    loginCount = APP.findClassCount(data, 'btn_text');
                    tryCount++;
                    await APP.sleep(1000);
                    continue;
                }
                if(tryCount > 3){
                    await APP.saveScreenshot_error(this.androidBot, 'login_按钮数量错误');
                    return false;
                }
            }
        }
        // 未登录
        jude = await this.waitLogin();
        return jude;
    }
    async waitLogin() {
        let loginInfo = this.GameClient.loginInfo;
        let data = await YoloDetector.screen(this.androidBot);
        let btnList = data.filter(item => item.className === 'btn_text');
        let click = false;
        let clickRegion = null;
        for (let i = 0; i < btnList.length; i++) {
            let btn = btnList[i];
            let region = btn.region;
            let ocrData = await APP.ocrRegion(this.androidBot, region);
            if (ocrData) {
                if (ocrData[0].text.includes(loginInfo.type) && ocrData[0].text.includes(loginInfo.phone)) {
                    APP.clickRegion(this.androidBot, region);
                    click = true;
                    clickRegion = region;
                    break;
                }
            }
        }
        if (!click) {
            APP.saveScreenshot_error(this.androidBot, 'login_按钮未找到');
            return false;
        }
        await APP.sleep(500);
        data = await YoloDetector.screen(this.androidBot);
        let haveTips = APP.findClass(data, 'game_tips');
        if (haveTips) {
            let ocrData = await APP.ocrRegion(this.androidBot, data[haveTips - 1].region);
            if (ocrData[0].text.includes('请勾选同意下方的服务协议，即可进入游戏')) {
                await APP.clickRegion(this.androidBot, [148, 454, 172, 483]);
                await APP.sleep(200);
                await APP.clickRegion(this.androidBot, clickRegion);
            }
        }
        APP.sleep(3000);
        // 开始扫码流程
        this.GameClient.addTask({name:'login/login_san'});
        return true;
        while (true) {
            let finds = await this.androidBot.findWords('扫码')
            if (finds) {
                scanCodeCreate = true;
                continue;
            }
            if (scanCodeCreate && !finds) {
                scanCode = true;
                break;
            }
            console.log('等待扫码');
            APP.sleep(1000);
        }
        console.log('扫码完成')
        return true;
    }
}

module.exports = index;